// pages/index/MeteorFall/Star_details/Star_details.js
var WxParse = require('../../../../wxParse/wxParse.js');
var wareId
var activityId
var orderRes
var url = wx.getStorageSync('url')
var openid = wx.getStorageSync('openid')
var userId = wx.getStorageSync('userId')
var shopid = wx.getStorageSync('shopid')
var time_ss = wx.getStorageSync('time_ss')
var set_time
var time
// 倒计时
// var second = 30

function countdown(that, second) {
  // var second = that.data.second

  if (second == 0) {
    that.setData({
      second: "0"
    });
    return;
    clearTimeout(time)
  }
  time = setTimeout(function () {
    that.setData({
      second: second - 1
    });
    second=second -1
    console.log(second)
    countdown(that, second);
  }
    , 1000)
};
Page({

  /**
   * 页面的初始数据
   */
  data: {
    idx:1,
    indicatorDots: false,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    currentTab:1,
    See_1:false,
    See_2: false,
    hiden:false,
    second:30
  },

  /**
   * 生命周期函数--监听页面加载
   */
  
  onLoad: function (options) {
    var shopid = wx.getStorageSync('shopid')
    var userId = wx.getStorageSync('userId')
    var openid = wx.getStorageSync('openid')
    var time_ss = wx.getStorageSync('time_ss')
    wareId = options.wareId
    activityId = options.activityId
    var that = this
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: url +'gateWare/mpWAInfo',
      data: {
        "platform": 2,
        "privId": "0000",
        "resId": "0000",
        "sendTime": time_ss,
        "seq": "232312313",
        "shopId": shopid,
        "source": 1,
        "sysVer": "0.0.1",
        "type": 1,
        "userId": userId,
        "userType": 1,
        "content": {
          "activityId": activityId,
          "wareId": wareId,
          "tag": "1"
        }
      },
      method: "POST",
      header: { "Content-Type": "application/JSON" },
      success: function (e) {
        var data = e.data.content
        console.log(data)
        wx.hideLoading()       
        orderRes = data.orderRes
        var orderRes1=[]
        if (orderRes.length<10){
          for (let i = 0; i < orderRes.length; i++) {
            orderRes1.push(orderRes[i])
          }
          that.setData({
            hiden:true,
          })
        }else{
          for (let i = 0; i < 10; i++) {
            orderRes1.push(orderRes[i])
          }
        }
        
        var length = e.data.content.bannerImg.length;
        var html = data.content.replace(/[\r\n]/g, "")
        WxParse.wxParse('article', 'html', html, that, 5);
        that.setData({
          datas: data,
          idxs: length,
          orderRes: orderRes1
        })
      }
    })
  },
  goto_shouchang:function(e){
    var shopid = wx.getStorageSync('shopid')
    var userId = wx.getStorageSync('userId')
    var openid = wx.getStorageSync('openid')
    var time_ss = wx.getStorageSync('time_ss') 
      wx.request({
        url: url + 'getUser/createCollect',
        data: {
          "list": false,
          "nodeId": "kdslfw",
          "platform": 2,
          "privId": "default",
          "resId": "default",
          "sendTime": time_ss,
          "seq": 1,
          "shopId": shopid,
          "source": 11,
          "sysVer": "1.0.0",
          "token": "kdsjfoewi12ijsldks",
          "type": 1,
          "userId": userId ,
          "userType": 1,
          "content": {
            "shopId": shopid,
            "userId": userId,
            "url": wareId + ',' + activityId,
            "collectClassify":5 ,
            "key": 'dasda',
            "collectType": 1,
          }
        },
        method: "POST",
        header: { "Content-Type": "application/JSON" },
        success:function(e){
          console.log(e.data.result)
          var i = e.data.result.desc;
          // var s = e.data.result.code.substring(4)
            wx.showLoading({
              title: i,
            })
            setTimeout(function () {
              wx.hideLoading()
            }, 1000)

        }
      })
  },
  genduo:function(){
    var that = this;
    that.setData({
      orderRes: orderRes,
      hiden:true
    })
  },
  bindchange_swiper:function(e){
    var i = e.detail.current+1
    var that = this
    that.setData({
      idx : i
    })
  },
  swichNav:function(e){
    var that = this
    that.setData({
      currentTab: e.currentTarget.dataset.current,
    })
  },
  See_1:function(){
    // clearTimeout(time)
    // countdown(this, 30);

    var that = this
    that.setData({
      See_1:true,
    })
  //  set_time = setTimeout(function(){
     

  //     wx.request({
  //       url: url + 'sharkActivity/release/{activityId}/{wareId}/{userId}',
  //       data: {},
  //       method: "POST",
  //       header: { "Content-Type": "application/json" },
  //       success: function (e) {
  //         console.log(e)
  //         that.setData({
  //           See_1: false,
  //           See_2: false,
  //         })
  //       }

  //     })
  //   },30000)
  },  
  See_2:function(e){
    var shopid = wx.getStorageSync('shopid')
    var userId = wx.getStorageSync('userId')
    var openid = wx.getStorageSync('openid')
    var time_ss = wx.getStorageSync('time_ss') 
    var that = this    
    var checkprice = e.currentTarget.dataset.checkprice //金额
      wx.request({
        url: url + 'sharkActivity/setnx/'+activityId+'/'+wareId+'/'+userId,
        data: {
        },
        method: "POST",
        header: { "Content-Type": "application/json" },
        success: function (e) {
          let s= e.data
          if(s == true){
            countdown(that, 30);
            set_time = setTimeout(function () {
              wx.request({
                url: url + 'sharkActivity/release/' + activityId + '/' + wareId + '/' + userId,
                data: {},
                method: "POST",
                header: { "Content-Type": "application/json" },
                success: function (e) {
                  console.log(e)
                  that.setData({
                    See_1: false,
                    See_2: false,
                  })
                }

              })
            }, 30000)

            wx.request({
              url: url + 'gateCash/payByMeteorFall',
              data: {
                "list": false,
                "nodeId": "kdslfw",
                "platform": 2,
                "privId": "default",
                "resId": "default",
                "sendTime": time_ss,
                "seq": 1,
                "shopId": shopid,
                "source": 11,
                "sysVer": "1.0.0",
                "token": "kdsjfoewi12ijsldks",
                "type": 1,
                "userId": userId,
                "userType": 1,
                "content": {
                  "shopId": shopid,
                  "openId": openid,
                  "appId": "wx6ca4bda138687ed8",
                  "activityId": activityId
                }
              },
              method: "POST",
              header: { "Content-Type": "application/json" },
              success: function (e) {
                console.log(e.data)
                var i_s = e.data.content
                var i = e.data.result.code.substring(4)
                console.log(i)
                if(i == '9999'){
                  wx.requestPayment({
                    timeStamp: i_s.timeStamp,
                    nonceStr: i_s.nonce_str,
                    package: 'prepay_id=' + i_s.prepay_id,
                    signType: 'MD5',
                    paySign: i_s.paySign,
                    success(res) { 
                      // clearTimeout(time)

                      wx.request({
                        url: url + 'gateCash/payByWxSuccess',
                        data: {
                          "list": false,
                          "nodeId": "kdslfw",
                          "platform": 2,
                          "privId": "default",
                          "resId": "default",
                          "sendTime": time_ss,
                          "seq": 1,
                          "shopId": shopid,
                          "source": 11,
                          "sysVer": "1.0.0",
                          "token": "kdsjfoewi12ijsldks",
                          "type": 1,
                          "userId": userId,
                          "userType": 1,
                          "content": {
                            "tradeId": i_s.tradeId
                          },
                          "page": 1,
                          "limit": 15
                        },
                        method: "POST",
                        header: { "Content-Type": "application/JSON" },
                        success: function (e) {
                          console.log(e)
                          that.setData({
                            See_1: false,
                            See_2: true,
                          })
                          wx.request({
                            url: url + 'sharkActivity/addLxjProcess',
                            data: {
                              "platform": 2,
                              "privId": "0000",
                              "resId": "0000",
                              "sendTime": time_ss,
                              "seq": "232312313",
                              "shopId": "1321",
                              "source": 1,
                              "sysVer": "0.0.1",
                              "type": 1,
                              "userId": userId,
                              "userType": 1,
                              "content": {
                                "activityId": activityId,
                                "wareId": wareId,
                                "payPrice": checkprice
                              }
                            },
                            method: "POST",
                            header: { "Content-Type": "application/json" },
                            success: function (e) {

                              console.log(e)
                              that.setData({
                                data_s: e.data.content
                              })
                            }
                          })
                        }
                      })
                    },
                    fail(res) { 
                      // clearTimeout(time)
                      wx.request({
                        url: url + 'sharkActivity/release/' + activityId + '/' + wareId + '/' + userId,
                        data: {},
                        method: "POST",
                        header: { "Content-Type": "application/json" },
                        success: function (e) {
                          console.log(e)
                          that.setData({
                            See_1: false,
                            See_2: false,
                          })
                        }

                      })
                    }
                  })
                }else if(i == '0000'){
                  that.setData({
                    See_1: false,
                    See_2: true,
                  });
                  wx.request({
                    url: url + 'sharkActivity/addActivityProcess',
                    data: {
                      "platform": 2,
                      "privId": "0000",
                      "resId": "0000",
                      "sendTime": time_ss,
                      "seq": "232312313",
                      "shopId": "1321",
                      "source": 1,
                      "sysVer": "0.0.1",
                      "type": 1,
                      "userId": userId,
                      "userType": 1,
                      "content": {
                        "activityId": activityId,
                        "wareId": wareId,
                        "payPrice": checkprice
                      }
                    },
                    method: "POST",
                    header: { "Content-Type": "application/json" },
                    success: function (e) {
                      console.log(e.data.content)
                      that.setData({
                        data_s: e.data.content
                      })
                    }
                  })
                }else{
                  
                  wx.request({
                    url: url + 'sharkActivity/release/' + activityId + '/' + wareId + '/' + userId,
                    data: {},
                    method: "POST",
                    header: { "Content-Type": "application/json" },
                    success: function (e) {
                      console.log(e)
                      that.setData({
                        See_1: false,
                        See_2: false,
                      })
                    }

                  })
                }
              }

            })
          }else{
            wx.showLoading({
              title: '已有用户正在查看',
            });
            setTimeout(function () {
              wx.hideLoading()
            }, 1000)
          }
        }
      })
     
  },
  guanbi_see_1:function(e){
    var that = this
    clearTimeout(time)
    wx.request({
      url: url + 'sharkActivity/release/' + activityId + '/' + wareId + '/' + userId,
      data: {},
      method: "POST",
      header: { "Content-Type": "application/json" },
      success: function (e) {
        that.setData({
          See_1: false,
          See_2: false,
        })
      }
    })
  },
  goto_shouye:function(){
    wx.switchTab({
      url:'../../../index/index'
    })
  },
  // 生成订单
  goto_OrderAddress:function(e){
    var that = this
    wx.request({
      url: url + 'sharkActivity/delay/' + activityId + '/' + wareId ,
      data: {},
      method: "POST",
      header: { "Content-Type": "application/json" },
      success: function (e) {
        console.log(e)
        that.setData({
          See_1: false,
          See_2: false,
        })
        wx.navigateTo({
          url: '../../../Order/OrderAddress/OrderAddress?wareId=' + wareId + '&activityId=' + activityId + '&tag=' + 1,
        })
      }
    })
   
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
   
    if (res.from === 'button') { }
    return {
      title: '转发',
      path: '/pages/index/index',
      success: function (e) {
        console.log(e)
       }
    }

  }
})